package com.jbm.jbmsupplier.presenters;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;

import com.jbm.jbmsupplier.adapter.OrderAdapter;
import com.jbm.jbmsupplier.modes.ListFreInterface;
import com.jbm.jbmsupplier.net.NetIntent;
import com.jbm.jbmsupplier.view.DialogLoading;
import com.jbm.jbmsupplier.views.ListFreViews;
import com.jbm.onehome.iface.IJOrderCtrl;
import com.jbm.onehome.iface.JobOrderEntity;

import java.util.ArrayList;

/**
 * Created by 家帮忙 on 2017/7/7.
 */

public class ListFrements implements ListFreInterface{
    private Context context;
    private ListFreViews listFreViews;
    private int page;
    private final int ALLORDER = 0001;
    private int num = 5;
    private  int icount = 0;
    private ArrayList<JobOrderEntity> listJorider;
    private NetIntent netintent;
    final String Order_Type_new = "new";
    final String Order_Type_break = "break";
    final String Order_Type_confirm = "confirm";
    final String Order_Type_submit = "submit";
    final String Order_Type_payed="payed";




    private String mQueryStatus;
    DialogLoading dialogloading;
    private boolean Isorder=false;
    private Broad_Orde broadorder;

    public ListFrements(Context context, ListFreViews listFreViews) {
        this.context = context;
        this.listFreViews = listFreViews;
    }
        public void setInitData(boolean Is){//初始化数据
          getInitLoadData(Is);
          }
    @Override
    public void getInitLoadData(final boolean Is) {//初始化数据
        listJorider = new ArrayList<>();
        netintent = new NetIntent(handler);
        if (icount == 0 && Is == true) {
            mQueryStatus = "'" + Order_Type_new + "','" + Order_Type_break + "'";
            netintent.setOrderDqr(mQueryStatus, page, num, ALLORDER);
            dialogloading = new DialogLoading(context, "加载中...", false);
            dialogloading.show();
        }
//        if (broadorder==null) {
            broadorder = new Broad_Orde();
            context.registerReceiver(broadorder, new IntentFilter("Isorder"));

//        }
    }

    @Override
    public void getOrderType(int i, String stat) {//滑动加载数据
//        icount=i;
        page = 0;
        mQueryStatus=stat;
        netintent.setOrderDqr(stat, page, num, ALLORDER);
    }
 public void setLoadLaunch(int tag){//加载上拉下拉数据
     getLoadLaunch(tag);
 }


    @Override
    public void getLoadLaunch(int tag) {
        if (tag==1){
            page += 5;
            netintent.setOrderDqr(mQueryStatus, page, num, ALLORDER);
        }else if (tag==2){
            page = 0;
            netintent.setOrderDqr(mQueryStatus, page, num, ALLORDER);
            dialogloading = new DialogLoading(context, "加载中...", false);
            dialogloading.show();
        }else if (tag==3){
            page += 5;
            netintent.setOrderDqr(mQueryStatus, page, num, ALLORDER);
            dialogloading = new DialogLoading(context, "加载中...", false);
            dialogloading.show();
        }

    }

    @Override
    public void onDestroy() {
        if (broadorder!=null){
            context.unregisterReceiver(broadorder);

        }
    }


    OrderAdapter orderAdapter;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case ALLORDER:
                    IJOrderCtrl jorder = (IJOrderCtrl) msg.obj;
                    if (jorder.m_code == 200) {
                        if (page == 0) {
                            listJorider.clear();
                            listFreViews.setUpdata(listJorider);

                            if (jorder.joblist != null) {
                                listJorider.addAll(jorder.joblist);
                                listFreViews.setOrderData(listJorider,1);//更新数据
                            }
                        } else {
                            if (jorder.joblist != null) {
                                if (jorder.joblist.size() > 0) {
                                    listJorider.addAll(jorder.joblist);
                                    listFreViews.setOrderData(listJorider,2);//更新数据
                                }
                            }
                        }
                        if (jorder.count == 0) {
                            listFreViews.setLoadLayout();
                        }
                       listFreViews.setSucced(dialogloading,listJorider);
                    } else if (jorder.m_code == 500) {
                        listFreViews.setError(jorder.m_info,dialogloading);
                    } else if (jorder.m_info.equals("Not Connect")) {
                        listFreViews.setError(jorder.m_info,dialogloading);
                    } else {
//                        listFreViews.setError(jorder.m_info,dialogloading);
                    }

                    break;
            }
        }
    };

class Broad_Orde extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (!intent.getStringExtra("name").equals("")) {

                if (intent.getStringExtra("name").equals("1")) {
                    page = 0;
                    mQueryStatus = "'" + Order_Type_new + "','" + Order_Type_break + "'";
                    netintent.setOrderDqr(mQueryStatus, page, num, ALLORDER);
                } else if (intent.getStringExtra("name").equals("2")) {
                    page = 0;
                    mQueryStatus = "'" + Order_Type_confirm + "'";
                    netintent.setOrderDqr(mQueryStatus, page, num, ALLORDER);

                } else if (intent.getStringExtra("name").equals("3")) {

                    page = 0;
                    mQueryStatus = "'" + Order_Type_submit + "'";
                    netintent.setOrderDqr(mQueryStatus, page, num, ALLORDER);

                }

            }
        }
    }




}
